trigger CalculateDistance on Branch__c (before update) {
	Id companyId = Trigger.new[0].Company__c;
	Company__c company = [Select Latitude__c, Longitude__c from Company__c where Id=: companyId];
	Double centreLat = Double.valueOf(company.Latitude__c)/57.29577951;
	Double centreLong = Double.valueOf(company.Longitude__c)/57.29577951;
	Double a, dLat, dLong, tempLat, tempLong, c;
	Double R = 6371;
	for(Integer i=0; i<Trigger.new.size(); i++){
		if(Trigger.new[i].City__c != Trigger.oldMap.get(Trigger.new[i].Id).City__c){
			Trigger.new[i].Latitude__c = '';
			Trigger.new[i].Longitude__c = '';
			Trigger.new[i].Distance__c = null;
		}
		if((Trigger.new[i].Latitude__c != Trigger.old[i].Latitude__c && Trigger.new[i].Latitude__c !=null) && (Trigger.new[i].Longitude__c != Trigger.old[i].Longitude__c && Trigger.new[i].Longitude__c != null)){
			tempLat = Double.valueOf(Trigger.new[i].Latitude__c)/57.29577951;
			tempLong = Double.valueOf(Trigger.new[i].Longitude__c)/57.29577951;
			System.debug('-------'+Double.valueOf(Trigger.new[i].Latitude__c)+'-------'+Double.valueOf(Trigger.new[i].Longitude__c)+'-------'+Double.valueOf(company.Latitude__c)+'-------'+Double.valueOf(company.Longitude__c));
			Trigger.new[i].Distance__c = GeoCodeHelper.calculateDistance(Double.valueOf(Trigger.new[i].Latitude__c), Double.valueOf(Trigger.new[i].Longitude__c), Double.valueOf(company.Latitude__c), Double.valueOf(company.Longitude__c));
			/*dLat = centreLat - tempLat;
			dLong = centreLong - tempLong;
			a =  Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(tempLat) * Math.cos(centreLat) * Math.sin(dLong/2) * Math.sin(dLong/2);
			c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));  
			Trigger.new[i].Distance__c = R*c;*/
		}
	}
}